package fhp.hlhj.giantfold.fragment
import android.content.Intent
import android.os.Bundle
import android.support.v4.app.Fragment
import android.support.v7.widget.GridLayoutManager
import android.support.v7.widget.LinearLayoutManager
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import com.bumptech.glide.Glide
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.BaseViewHolder
import com.lzy.okgo.model.HttpParams
import de.hdodenhof.circleimageview.CircleImageView
import fhp.hlhj.giantfold.R
import fhp.hlhj.giantfold.activity.camera.Camera2Aty
import fhp.hlhj.giantfold.activity.cameraScan.CameraNotAty
import fhp.hlhj.giantfold.activity.cameraScan.MerchantAty
import fhp.hlhj.giantfold.activity.orders.TiketAty
import fhp.hlhj.giantfold.activity.user.OrderAty
import fhp.hlhj.giantfold.customView.FullyGridLayoutManager
import fhp.hlhj.giantfold.customView.FullyLinearLayoutManager
import fhp.hlhj.giantfold.customView.LoadingDialog
import fhp.hlhj.giantfold.customView.TittleGridAdp
import fhp.hlhj.giantfold.interfaces.ISellers
import fhp.hlhj.giantfold.interfaces.ISpendBuy
import fhp.hlhj.giantfold.javaBean.MerchantBean
import fhp.hlhj.giantfold.javaBean.SellersBean
import fhp.hlhj.giantfold.javaBean.TittleBean
import fhp.hlhj.giantfold.network.Urls
import fhp.hlhj.giantfold.presenter.SellersPresenter
import fhp.hlhj.giantfold.presenter.SpendBuyPresenter
import fhp.hlhj.giantfold.utils.LoadingUtils
import fhp.hlhj.giantfold.utils.LogUtil
import fhp.hlhj.giantfold.utils.MyUtils
import kotlinx.android.synthetic.main.fgm_spend_local.*
import kotlinx.android.synthetic.main.fgm_spend_local.view.*
/**
 * author: FHP
 * data: on 2017\11\22 0022 10:44
 * E-Mail: 103945089@qq.com
 */
class SpendLocalFgm :Fragment(),ISpendBuy,ISellers {
    private lateinit var rootView:View
    private lateinit var tittleAdp:TittleGridAdp
    private lateinit var sellesPresenter:SellersPresenter
    private lateinit var loadingDialog:LoadingDialog
    private lateinit var presenter:SpendBuyPresenter
    private  var datas=ArrayList<TittleBean.DataBean>()
    private val gridDatas=java.util.ArrayList<SellersBean.DataBean>()
    private val listDatas=java.util.ArrayList<SellersBean.DataBean>()
    private lateinit var listAdp:BaseQuickAdapter<SellersBean.DataBean,BaseViewHolder>
    private lateinit var gridAdp:BaseQuickAdapter<SellersBean.DataBean,BaseViewHolder>
    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
        rootView=inflater.inflate(R.layout.fgm_spend_local,null)
        loadingDialog=LoadingUtils.getInstence(context)
        presenter= SpendBuyPresenter(this)
        sellesPresenter= SellersPresenter(this)
        tittleAdp= TittleGridAdp(context,datas)
        setGridView()
        val params = HttpParams()
        params.put("type",2)
        presenter.getTittles(params)
        rootView.tittleGrid.adapter=tittleAdp
        initListener()
        return rootView
    }
    //设置商户gridView
    private fun setGridView() {
        gridAdp =object :BaseQuickAdapter<SellersBean.DataBean,BaseViewHolder>(R.layout.seller_item,gridDatas){
            override fun convert(helper: BaseViewHolder?, item: SellersBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.name)
                helper?.setText(R.id.itemInfo,item?.description)
                val imageView = helper?.getView<ImageView>(R.id.itemIcon)
                Glide.with(mContext).load(Urls.BaseUrl+item?.savepath+item?.savename).asBitmap().dontAnimate().placeholder(R.mipmap.exp7).into(imageView)
            }
        }
        gridAdp.setOnItemClickListener { adapter, view, position ->
            val intent = Intent(context, MerchantAty::class.java)
            intent.putExtra("pid",gridDatas[position].id)
            intent.putExtra("name",gridDatas[position].name)
            context.startActivity(intent)
        }
        rootView.gridView.layoutManager=GridLayoutManager(context,2)
        rootView.gridView.adapter=gridAdp
        sellesPresenter.getSeller()
        //下部的listView设置
        rootView.listView.layoutManager=LinearLayoutManager(context,LinearLayout.VERTICAL,false)
        listAdp=object :BaseQuickAdapter<SellersBean.DataBean,BaseViewHolder>(R.layout.bottom_seller_item,listDatas){
            override fun convert(helper: BaseViewHolder?, item: SellersBean.DataBean?) {
                helper?.setText(R.id.itemTittle,item?.name)
                helper?.setText(R.id.itemInfo,item?.description)
                val imageView = helper?.getView<CircleImageView>(R.id.itemIcon)
                Glide.with(mContext).load(Urls.BaseUrl+item?.savepath+item?.savename).asBitmap().dontAnimate().placeholder(R.mipmap.placeholdericon).into(imageView)
            }
        }
        rootView.listView.adapter=listAdp
    }

    private fun initListener() {
        rootView.btTakePhoto.setOnClickListener {
            MyUtils.toast("请选择商户进去拍小票哟")
//            context.startActivity(Intent(activity,Camera2Aty::class.java))
        }
        rootView.tittleGrid.setOnItemClickListener { parent, view, position, id ->
            val intent = Intent(activity, MerchantAty::class.java)
            intent.putExtra("pid",datas[position].id)
            context.startActivity(intent)
        }

        rootView.btRule.setOnClickListener {
            context.startActivity(Intent(context,CameraNotAty::class.java))
        }
        rootView.btMyK.setOnClickListener {
            context.startActivity(Intent(context,TiketAty::class.java))
        }
    }

    override fun showLoading() {
        loadingDialog.show()
    }

    override fun getFirst(tittleBean: TittleBean) {
        loadingDialog.dismiss()
        datas.clear()
        datas.addAll(tittleBean.data)
        tittleAdp.notifyDataSetChanged()
    }

    override fun getSellers(sellersBean: SellersBean) {
        if (sellersBean.code==200){
//            MyUtils.toast("有数据没有?${sellersBean.data.size}")
            gridDatas.clear()
            listDatas.clear()
            if (sellersBean.data.size>4){
                LogUtil.log("进入1")
                gridDatas.add(sellersBean.data[0])
                gridDatas.add(sellersBean.data[1])
                gridDatas.add(sellersBean.data[2])
                gridDatas.add(sellersBean.data[3])
                gridAdp.notifyDataSetChanged()

                listDatas.addAll(sellersBean.data)
                listDatas.removeAt(0)
                listDatas.removeAt(0)
                listDatas.removeAt(0)
                listDatas.removeAt(0)
                listAdp.notifyDataSetChanged()
            }else{
                LogUtil.log("进入2${sellersBean.data.size}")
                gridDatas.addAll(sellersBean.data)
                gridAdp.notifyDataSetChanged()
            }
        }

    }

    /**
     * 无用
     */
    override fun getFindGoods(tittleBean: TittleBean) {
    }
    //无用
    override fun getMerchant(merchantBean: MerchantBean) {
    }
}